package com;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 用于生成密码
 * @author 867101835
 *
 */
public class PasswordCreate {

	public static void main(String[] args) {

		try {
			String password = "123456";
			String passwordD = new PasswordCreate().toMD5(password);
			System.out.println("加密前：" + password + ";加密后：" + passwordD);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	private String toMD5(String password) {
		// md5 encrypt
		MessageDigest md5;
		StringBuffer md5StrBuff = new StringBuffer();
		try {
			md5 = MessageDigest.getInstance("MD5");
			md5.update(password.getBytes());
			byte[] domain = md5.digest();
			// converting domain to String
			for (int i = 0; i < domain.length; i++) {
				if (Integer.toHexString(0xFF & domain[i]).length() == 1) {
					md5StrBuff.append("0").append(
							Integer.toHexString(0xFF & domain[i]));
				} else
					md5StrBuff.append(Integer.toHexString(0xFF & domain[i]));
			}
			// System.out.println(md5StrBuff.toString());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return md5StrBuff.toString();
	}

}
